<?php

require_once 'Fields.php';
require_once 'Units.php';
require_once 'UnitTypes.php';
require_once 'Users.php';

class Procedures {

    // các thuộc tính
    private $_ID;
    private $_title;
    private $_alias;
    private $_createdDate;
    private $_modifiedDate;
    private $_description;
    private $_content;
    private $_image;
    private $_imageStatus;
    private $_status;
    private $_order;
    private $_hits;
    private $_highLight;
    private $_createdUserID;
    private $_modifiedUserID;
    private $_limit;
    private $_unitTypeName;
    private $_fieldID;
    private $_unitID;
    // các đối tượng

    private $_createdUser;
    private $_modifiedUser;
    private $_unitType;
    private $_field;
    private $_unit;

    public function getUnitTypeName() {
        return $this->_unitTypeName;
    }

    public function getFieldID() {
        return $this->_fieldID;
    }

    public function getUnitID() {
        return $this->_unitID;
    }

    public function getUnitType() {
        return $this->_unitType;
    }

    public function getField() {
        return $this->_field;
    }

    public function getUnit() {
        return $this->_unit;
    }

    public function setUnitTypeName($UnitTypeName) {
        $this->_unitTypeName = $UnitTypeName;
    }

    public function setFieldID($FieldID) {
        $this->_fieldID = $FieldID;
    }

    public function setUnitID($UnitID) {
        $this->_unitID = $UnitID;
    }

    public function setUnitType($UnitType) {
        $this->_unitType = $UnitType;
    }

    public function setField($Field) {
        $this->_field = $Field;
    }

    public function setUnit($Unit) {
        $this->_unit = $Unit;
    }

    public function getLimit() {
        return $this->_limit;
    }

    public function setLimit($Limit) {
        $this->_limit = $Limit;
    }

    public function getID() {
        return $this->_ID;
    }

    public function setID($ID) {
        $this->_ID = $ID;
    }

    public function getTitle() {
        return $this->_title;
    }

    public function setTitle($title) {
        $this->_title = $title;
    }

    public function getAlias() {
        return $this->_alias;
    }

    public function setAlias($alias) {
        $this->_alias = $alias;
    }

    public function getCreatedDate() {
        return $this->_createdDate;
    }

    public function setCreatedDate($createdDate) {
        $this->_createdDate = $createdDate;
    }

    public function getModifiedDate() {
        return $this->_modifiedDate;
    }

    public function setModifiedDate($modifiedDate) {
        $this->_modifiedDate = $modifiedDate;
    }

    public function getDescription() {
        return $this->_description;
    }

    public function setDescription($description) {
        $this->_description = $description;
    }

    public function getContent() {
        return $this->_content;
    }

    public function setContent($content) {
        $this->_content = $content;
    }

    public function getImage() {
        return $this->_image;
    }

    public function setImage($image) {
        $this->_image = $image;
    }

    public function getImageStatus() {
        return $this->_imageStatus;
    }

    public function setImageStatus($imageStatus) {
        $this->_imageStatus = $imageStatus;
    }

    public function getStatus() {
        return $this->_status;
    }

    public function setStatus($status) {
        $this->_status = $status;
    }

    public function getOrder() {
        return $this->_order;
    }

    public function setOrder($order) {
        $this->_order = $order;
    }

    public function getHits() {
        return $this->_hits;
    }

    public function setHits($hits) {
        $this->_hits = $hits;
    }

    public function getHighLight() {
        return $this->_highLight;
    }

    public function setHighLight($highLight) {
        $this->_highLight = $highLight;
    }

    public function getCreatedUserID() {
        return $this->_createdUserID;
    }

    public function setCreatedUserID($createdUserID) {
        $this->_createdUserID = $createdUserID;
    }

    public function getModifiedUserID() {
        return $this->_modifiedUserID;
    }

    public function setModifiedUserID($modifiedUserID) {
        $this->_modifiedUserID = $modifiedUserID;
    }

    public function getCategory() {
        return $this->_category;
    }

    public function setCategory($category) {
        $this->_category = $category;
    }

    public function getCreatedUser() {
        return $this->_createdUser;
    }

    public function setCreatedUser($createdUser) {
        $this->_createdUser = $createdUser;
    }

    public function getModifiedUser() {
        return $this->_modifiedUser;
    }

    public function setModifiedUser($modifiedUser) {
        $this->_modifiedUser = $modifiedUser;
    }

    public function getHighLightList($db = null) {
        if ($db != null) {
            $sqlQuery = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`
                          WHERE P_HighLight = 1
                          ORDER BY P_Order DESC";

            $parameters = null;
            $values = null;
            $types = null;

            $result = $db->executeQuery($sqlQuery, $parameters, $values, $types);


            if ($result != null) {
                $rows = array();
                while ($row = $result->fetch()) {

                    $procedure = new Procedures();
                    $procedure->setID($row->P_ID);
                    $procedure->setTitle($row->P_Title);
                    $procedure->setAlias($row->P_Alias);
                    $procedure->setCreatedDate($row->P_CreatedDate);
                    $procedure->setDescription($row->P_Description);
                    $procedure->setContent($row->P_Content);
                    $procedure->setImage($row->P_Image);
                    $procedure->setImageStatus($row->P_ImageStatus);
                    $procedure->setModifiedDate($row->P_ModifiedDate);
                    $procedure->setStatus($row->P_Status);
                    $procedure->setOrder($row->P_Order);
                    $procedure->setHits($row->P_Hits);
                    $procedure->setHighLight($row->P_HighLight);

                    $procedure->setUnitTypeName($row->P_UT_Name);
                    $procedure->setUnitID($row->P_U_ID);
                    $procedure->setFieldID($row->P_F_ID);


                    $procedure->setCreatedUserID($row->P_CreatedUserID);
                    $procedure->setModifiedUserID($row->P_ModifiedUserID);


                    // created user
                    $createdUser = new Users();
                    $createdUser->setID($procedure->getCreatedUserID());
                    $createdUser->getByID($db);
                    $procedure->setCreatedUser($createdUser);


                    // modified user
                    $modifiedUser = new Users();
                    $modifiedUser->setID($procedure->getModifiedUserID());
                    $modifiedUser->getByID($db);
                    $procedure->setModifiedUser($modifiedUser);


                    // unit
                    $unit = new Units();
                    $unit->setID($procedure->getUnitID());
                    $unit = $unit->getByID($db);
                    $procedure->setUnit($unit);

                    // field
                    $field = new Fields();
                    $field->setID($procedure->getFieldID());
                    $field->getByID($db);
                    $procedure->setField($Field);


                    $rows[] = $procedure;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    public function getList($db) {
        if ($db != null) {

            $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`                     
                    ORDER BY P_Order DESC";

            $parameters = null;
            $values = null;
            $types = null;

            if ($this->getUnitTypeName() != '') {

                if ($this->getFieldID() != 0 && $this->getUnitID() != 0) {
                    $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`     
                          WHERE P_F_ID = :P_F_ID
                          AND P_U_ID = :P_U_ID
                          AND P_UT_Name = :P_UT_Name
                    ORDER BY P_Order DESC";

                    $parameters = array(":P_F_ID", ":P_U_ID", ":P_UT_Name");
                    $types = array(PDO::PARAM_INT, PDO::PARAM_INT, PDO::PARAM_STR);
                    $values = array($this->getFieldID(), $this->getUnitID(), $this->getUnitTypeName());
                } else if ($this->getFieldID() == 0 && $this->getUnitID() != 0) {
                    $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`     
                          WHERE P_U_ID = :P_U_ID
                          AND P_UT_Name = :P_UT_Name
                    ORDER BY P_Order DESC";

                    $parameters = array(":P_U_ID", ":P_UT_Name");
                    $types = array(PDO::PARAM_INT, PDO::PARAM_STR);
                    $values = array($this->getUnitID(), $this->getUnitTypeName());
                } else if ($this->getFieldID() != 0 && $this->getUnitID() == 0) {
                    $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`     
                          WHERE P_F_ID = :P_F_ID
                          AND P_UT_Name = :P_UT_Name
                    ORDER BY P_Order DESC";

                    $parameters = array(":P_F_ID", ":P_UT_Name");
                    $types = array(PDO::PARAM_INT, PDO::PARAM_STR);
                    $values = array($this->getFieldID(), $this->getUnitTypeName());
                } else {
                    $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`     
                          WHERE P_UT_Name = :P_UT_Name
                    ORDER BY P_Order DESC";

                    $parameters = array(":P_UT_Name");
                    $types = array(PDO::PARAM_STR);
                    $values = array($this->getUnitTypeName());
                }
            } else {
                if ($this->getFieldID() != 0 && $this->getUnitID() != 0) {
                    $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`     
                          WHERE P_F_ID = :P_F_ID
                          AND P_U_ID = :P_U_ID
                    ORDER BY P_Order DESC";

                    $parameters = array(":P_F_ID", ":P_U_ID");
                    $types = array(PDO::PARAM_INT, PDO::PARAM_INT);
                    $values = array($this->getFieldID(), $this->getUnitID());
                } else if ($this->getFieldID() == 0 && $this->getUnitID() != 0) {
                    $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`     
                          WHERE P_U_ID = :P_U_ID
                    ORDER BY P_Order DESC";

                    $parameters = array(":P_U_ID");
                    $types = array(PDO::PARAM_INT);
                    $values = array($this->getUnitID());
                } else if ($this->getFieldID() != 0 && $this->getUnitID() == 0) {
                    $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`     
                          WHERE P_F_ID = :P_F_ID
                    ORDER BY P_Order DESC";

                    $parameters = array(":P_F_ID");
                    $types = array(PDO::PARAM_INT);
                    $values = array($this->getFieldID());
                }
            }




            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $procedure = new Procedures();
                    $procedure->setID($row->P_ID);
                    $procedure->setTitle($row->P_Title);
                    $procedure->setAlias($row->P_Alias);
                    $procedure->setCreatedDate($row->P_CreatedDate);
                    $procedure->setDescription($row->P_Description);
                    $procedure->setContent($row->P_Content);
                    $procedure->setImage($row->P_Image);
                    $procedure->setImageStatus($row->P_ImageStatus);
                    $procedure->setModifiedDate($row->P_ModifiedDate);
                    $procedure->setStatus($row->P_Status);
                    $procedure->setOrder($row->P_Order);
                    $procedure->setHits($row->P_Hits);
                    $procedure->setHighLight($row->P_HighLight);

                    $procedure->setUnitTypeName($row->P_UT_Name);
                    $procedure->setUnitID($row->P_U_ID);
                    $procedure->setFieldID($row->P_F_ID);


                    $procedure->setCreatedUserID($row->P_CreatedUserID);
                    $procedure->setModifiedUserID($row->P_ModifiedUserID);


                    // created user
                    $createdUser = new Users();
                    $createdUser->setID($procedure->getCreatedUserID());
                    $createdUser->getByID($db);
                    $procedure->setCreatedUser($createdUser);


                    // modified user
                    $modifiedUser = new Users();
                    $modifiedUser->setID($procedure->getModifiedUserID());
                    $modifiedUser->getByID($db);
                    $procedure->setModifiedUser($modifiedUser);


                    // unit
                    $unit = new Units();
                    $unit->setID($procedure->getUnitID());
                    $unit = $unit->getByID($db);
                    $procedure->setUnit($unit);

                    // field
                    $field = new Fields();
                    $field->setID($procedure->getFieldID());
                    $field->getByID($db);
                    $procedure->setField($field);


                    $rows[] = $procedure;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    public function getListByUnitIDAndFieldID($db) {
        if ($db != null) {
            $sql = "SELECT
                            `P_ID`,
                            `P_Title`,
                            `P_Alias`,
                            `P_CreatedDate`,
                            `P_Description`,
                            `P_Content`,
                            `P_Image`,
                            `P_ImageStatus`,
                            `P_ModifiedDate`,
                            `P_Status`,
                            `P_Order`,
                            `P_Hits`,
                            `P_HighLight`,
                            `P_CreatedUserID`,
                            `P_ModifiedUserID`,
                            `P_UT_Name`,
                            `P_F_ID`,
                            `P_U_ID`
                          FROM `procedures`                     
                    ORDER BY P_Order DESC";

            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $procedure = new Procedures();
                    $procedure->setID($row->P_ID);
                    $procedure->setTitle($row->P_Title);
                    $procedure->setAlias($row->P_Alias);
                    $procedure->setCreatedDate($row->P_CreatedDate);
                    $procedure->setDescription($row->P_Description);
                    $procedure->setContent($row->P_Content);
                    $procedure->setImage($row->P_Image);
                    $procedure->setImageStatus($row->P_ImageStatus);
                    $procedure->setModifiedDate($row->P_ModifiedDate);
                    $procedure->setStatus($row->P_Status);
                    $procedure->setOrder($row->P_Order);
                    $procedure->setHits($row->P_Hits);
                    $procedure->setHighLight($row->P_HighLight);

                    $procedure->setUnitTypeName($row->P_UT_Name);
                    $procedure->setUnitID($row->P_U_ID);
                    $procedure->setFieldID($row->P_F_ID);


                    $procedure->setCreatedUserID($row->P_CreatedUserID);
                    $procedure->setModifiedUserID($row->P_ModifiedUserID);


                    // created user
                    $createdUser = new Users();
                    $createdUser->setID($procedure->getCreatedUserID());
                    $createdUser->getByID($db);
                    $procedure->setCreatedUser($createdUser);


                    // modified user
                    $modifiedUser = new Users();
                    $modifiedUser->setID($procedure->getModifiedUserID());
                    $modifiedUser->getByID($db);
                    $procedure->setModifiedUser($modifiedUser);


                    // unit
                    $unit = new Units();
                    $unit->setID($procedure->getUnitID());
                    $unit = $unit->getByID($db);
                    $procedure->setUnit($unit);

                    // field
                    $field = new Fields();
                    $field->setID($procedure->getFieldID());
                    $field->getByID($db);
                    $procedure->setField($field);


                    $rows[] = $procedure;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    public function searchByTitle($db) {
        if ($db != null) {
            $sql = "select
                        `P_ID`,
                        `P_Title`,
                        `P_Alias`,
                        `P_CreatedDate`,
                        `P_Description`,
                        `P_Content`,
                        `P_Image`,
                        `P_ImageStatus`,
                        `P_ModifiedDate`,
                        `P_Status`,
                        `P_Order`,
                        `P_Hits`,
                        `P_HighLight`,
                        `P_CreatedUserID`,
                        `P_ModifiedUserID`,
                        `P_UT_Name`,
                        `P_F_ID`,
                        `P_U_ID`
                      from `procedures`
                      where P_Title like :P_Title";

            $parameters = array(":P_Title");
            $types = array(PDO::PARAM_STR);
            $values = array('%' . $this->getTitle() . '%');

            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $procedure = new Procedures();
                    $procedure->setID($row->P_ID);
                    $procedure->setTitle($row->P_Title);
                    $procedure->setAlias($row->P_Alias);
                    $procedure->setCreatedDate($row->P_CreatedDate);
                    $procedure->setDescription($row->P_Description);
                    $procedure->setContent($row->P_Content);
                    $procedure->setImage($row->P_Image);
                    $procedure->setImageStatus($row->P_ImageStatus);
                    $procedure->setModifiedDate($row->P_ModifiedDate);
                    $procedure->setStatus($row->P_Status);
                    $procedure->setOrder($row->P_Order);
                    $procedure->setHits($row->P_Hits);
                    $procedure->setHighLight($row->P_HighLight);

                    $procedure->setUnitTypeName($row->P_UT_Name);
                    $procedure->setUnitID($row->P_U_ID);
                    $procedure->setFieldID($row->P_F_ID);


                    $procedure->setCreatedUserID($row->P_CreatedUserID);
                    $procedure->setModifiedUserID($row->P_ModifiedUserID);


                    // created user
                    $createdUser = new Users();
                    $createdUser->setID($procedure->getCreatedUserID());
                    $createdUser->getByID($db);
                    $procedure->setCreatedUser($createdUser);


                    // modified user
                    $modifiedUser = new Users();
                    $modifiedUser->setID($procedure->getModifiedUserID());
                    $modifiedUser->getByID($db);
                    $procedure->setModifiedUser($modifiedUser);


                    // unit
                    $unit = new Units();
                    $unit->setID($procedure->getUnitID());
                    $unit = $unit->getByID($db);
                    $procedure->setUnit($unit);

                    // field
                    $field = new Fields();
                    $field->setID($procedure->getFieldID());
                    $field->getByID($db);
                    $procedure->setField($field);


                    $rows[] = $procedure;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

    public function add($db) {
        if ($db != null) {
            $sql = "insert into `procedures`
                                (
                                 `P_Title`,
                                 `P_Alias`,
                                 `P_CreatedDate`,
                                 `P_Description`,
                                 `P_Content`,
                                 `P_Image`,
                                 `P_ImageStatus`,
                                 `P_ModifiedDate`,
                                 `P_Status`,
                                 `P_Order`,
                                 `P_Hits`,
                                 `P_HighLight`,
                                 `P_CreatedUserID`,
                                 `P_ModifiedUserID`,
                                 `P_UT_Name`,
                                 `P_F_ID`,
                                 `P_U_ID`)
                    values (
                            :P_Title,
                            :P_Alias,
                            :P_CreatedDate,
                            :P_Description,
                            :P_Content,
                            :P_Image,
                            :P_ImageStatus,
                            :P_ModifiedDate,
                            :P_Status,
                            :P_Order,
                            :P_Hits,
                            :P_HighLight,
                            :P_CreatedUserID,
                            :P_ModifiedUserID,
                            :P_UT_Name,
                            :P_F_ID,
                            :P_U_ID)";
            $parameters = array(
                ':P_Title', ':P_Alias',
                ':P_CreatedDate', ':P_Description',
                ':P_Content', ':P_Image',
                ':P_ImageStatus', ':P_ModifiedDate',
                ':P_Status', ':P_Order',
                ':P_Hits', ':P_HighLight',
                ':P_CreatedUserID', ':P_ModifiedUserID',
                ':P_UT_Name', ':P_F_ID',
                ':P_U_ID'
            );
            $values = array(
                $this->getTitle(), $this->getAlias(),
                $this->getCreatedDate(), $this->getDescription(),
                $this->getContent(), $this->getImage(),
                $this->getImageStatus(), $this->getModifiedDate(),
                $this->getStatus(), $this->getMaxOrder($db),
                $this->getHits(), 0,
                $this->getCreatedUserID(), $this->getModifiedUserID(),
                $this->getUnitTypeName(), $this->getFieldID(),
                $this->getUnitID()
            );
            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT, PDO::PARAM_STR,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_INT
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function delete($db) {
        if ($db != null) {
            $sql = "DELETE
                    FROM `procedures`
                    WHERE `P_ID` = :P_ID;";
            $parameters = array(':P_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    public function update($db) {
        if ($db != null) {
            $sql = "UPDATE `procedures`
                    SET 
                      `P_Title` = :P_Title,
                      `P_Alias` = :P_Alias,                      
                      `P_Description` = :P_Description,
                      `P_Content` = :P_Content,
                      `P_Image` = :P_Image,
                      `P_ImageStatus` = :P_ImageStatus,
                      `P_ModifiedDate` = :P_ModifiedDate,
                      `P_Status` = :P_Status,
                      `P_ModifiedUserID` = :P_ModifiedUserID,
                      `P_UT_Name` = :P_UT_Name,
                      `P_F_ID` = :P_F_ID,
                      `P_U_ID` = :P_U_ID
                    WHERE `P_ID` = :P_ID;";

            $parameters = array(
                ':P_Title', ':P_Alias',
                ':P_Description',
                ':P_Content', ':P_Image',
                ':P_ImageStatus', ':P_ModifiedDate',
                ':P_Status',
                ':P_ModifiedUserID',
                ':P_UT_Name', ':P_F_ID',
                ':P_U_ID', ':P_ID'
            );

            $values = array(
                $this->getTitle(), $this->getAlias(),
                $this->getDescription(),
                $this->getContent(), $this->getImage(),
                $this->getImageStatus(), $this->getModifiedDate(),
                $this->getStatus(),
                $this->getModifiedUserID(),
                $this->getUnitTypeName(), $this->getFieldID(),
                $this->getUnitID(), $this->getID()
            );

            $types = array(
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_STR,
                PDO::PARAM_STR, PDO::PARAM_STR,
                PDO::PARAM_INT, PDO::PARAM_STR,
                PDO::PARAM_INT,
                PDO::PARAM_INT,
                PDO::PARAM_STR, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT
            );


            return $db->executeNonQuery($sql, $parameters, $values, $types);
        }
    }

    // tải chi tiết 1 bài viết bởi mã bài viết
    public function getByID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                    `P_ID`,
                    `P_Title`,
                    `P_Alias`,
                    `P_CreatedDate`,
                    `P_Description`,
                    `P_Content`,
                    `P_Image`,
                    `P_ImageStatus`,
                    `P_ModifiedDate`,
                    `P_Status`,
                    `P_Order`,
                    `P_Hits`,
                    `P_HighLight`,
                    `P_CreatedUserID`,
                    `P_ModifiedUserID`,
                    `P_UT_Name`,
                    `P_F_ID`,
                    `P_U_ID`
                  FROM `procedures`
                  WHERE P_ID = :P_ID";

            $parameters = array(':P_ID');
            $values = array($this->getID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            // nếu có kết quả
            if ($result != null) {

                $row = $result->fetch();
                $this->setID($row->P_ID);
                $this->setTitle($row->P_Title);
                $this->setAlias($row->P_Alias);
                $this->setCreatedDate($row->P_CreatedDate);
                $this->setDescription($row->P_Description);
                $this->setContent($row->P_Content);
                $this->setImage($row->P_Image);
                $this->setImageStatus($row->P_ImageStatus);
                $this->setModifiedDate($row->P_ModifiedDate);
                $this->setStatus($row->P_Status);
                $this->setOrder($row->P_Order);
                $this->setHits($row->P_Hits);
                $this->setHighLight($row->P_HighLight);

                $this->setUnitTypeName($row->P_UT_Name);
                $this->setUnitID($row->P_U_ID);
                $this->setFieldID($row->P_F_ID);


                $this->setCreatedUserID($row->P_CreatedUserID);
                $this->setModifiedUserID($row->P_ModifiedUserID);


                // created user
                $createdUser = new Users();
                $createdUser->setID($this->getCreatedUserID());
                $createdUser->getByID($db);
                $this->setCreatedUser($createdUser);


                // modified user
                $modifiedUser = new Users();
                $modifiedUser->setID($this->getModifiedUserID());
                $modifiedUser->getByID($db);
                $this->setModifiedUser($modifiedUser);


                // unit
                $unit = new Units();
                $unit->setID($this->getUnitID());
                $unit = $unit->getByID($db);
                $this->setUnit($unit);

                // field
                $field = new Fields();
                $field->setID($this->getFieldID());
                $field->getByID($db);
                $this->setField($field);

                return $this;
            } else {
                return null;
            }
        }
    }

    public function getMaxOrder($db) {
        if ($db != null) {
            $sql = "SELECT MAX(P_Order) + 1 AS MaxOrder FROM procedures";
            $parameters = null;
            $values = null;
            $types = null;
            $result = $db->executeQuery($sql, $parameters, $values, $types);

            $row = $result->fetch();
            if ($row->MaxOrder != null) {
                return $row->MaxOrder;
            } else {
                return 1;
            }
        }
    }

}

?>
